<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>设备分布图</title>
</head>
<script charset="utf-8"
        src="https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
<script src="/static/home/js/jquery.min.js"></script>
<style type="text/css">
    html,
    body {
        height: 98%;
        margin: 0px;
        padding: 0px;
    }

    #container {
        width: 95%;
        height: 88%;
        margin: 0 auto;
    }
</style>

<body onload="initMap()">

<h3 style="margin-left: 45px">设备分布点位图</h3>
<h4 style="margin-left: 45px">设备总数：<span style="color: red" id="total">0</span></h4>
<div id="container"></div>
<script>
    function initMap() {
        $.post('/admin/index/map', {}, function(res) {
            var points = res.data;
            $('#total').html(res.msg);
            var center = new TMap.LatLng(34.769475, 113.665948);//设置中心点坐标
            //初始化地图
            var map = new TMap.Map("container", {
                center: center
            });
            var json = {
                id: 'marker-layer',
                map: map,
                styles: {
                    "marker": new TMap.MarkerStyle({
                        "width": 34,
                        "height": 35,
                        "anchor": { x: 12, y: 35 },
                        "src":'/static/api/img/device.png'
                        // "src": 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png'
                    })
                }
            };
            var geometries = [];
            for (var i = 0; i < points.length; i++) {
                var _html = '<div>售酒端:'+points[i].user_name+'</div><div>联系电话:'+points[i].mobile+'</div><div>设备地址:'+points[i].address+'</div>';
                geometries.push({
                    "id": points[i].id,
                    "styleId": 'marker',
                    "position": new TMap.LatLng(points[i].lat, points[i].lng),
                    "properties": {
                        "title": _html
                    }
                });
            }
            json.geometries = geometries;
            //初始marker
            var marker = new TMap.MultiMarker(json);
            //初始化infoWindow
            var infoWindow = new TMap.InfoWindow({
                map: map,
                position: new TMap.LatLng(39.984104, 116.307503),
                offset: { x: 0, y: -32 } //设置信息窗相对position偏移像素
            });
            infoWindow.close();//初始关闭信息窗关闭
            //监听标注点击事件
            marker.on("click", function (evt) {
                //设置infoWindow
                infoWindow.open(); //打开信息窗
                infoWindow.setPosition(evt.geometry.position);//设置信息窗位置
                infoWindow.setContent(evt.geometry.properties.title);//设置信息窗内容
            })
        },'json');

    }
</script>
</body>

</html>